<?php


namespace app\bock\type\action;


use app\bock\type\bock\IBock;
use app\bock\type\INode;
use app\bock\type\NodeBackGround;
use app\bock\type\NodeStack;
use app\bock\type\Point;
use app\bock\type\pre\NowBock;
use app\bock\type\stack\PanelStack;

class Operating
{
    /**
     * @var NowBock
     */
    protected $nowBock;

    /**
     * @var PanelStack
     */
    protected $panelStack;

    /**
     * @param NowBock $nowBock
     */
    public function setNowBock($nowBock)
    {
        $this->nowBock = $nowBock;
    }

    /**
     * @param PanelStack $panelStack
     */
    public function setPanelStack($panelStack)
    {
        $this->panelStack = $panelStack;
    }

    public function moveLeft()
    {
        $point = $this->nowBock->getBock()->getStartPoint();

        $nodes = $this->nowBock->getBock()->getAllEffNode();

        foreach ($nodes as $item)
        {
            /**
             * @var INode $item
             */
            // 左边是null 或者 stack 则不能移动
            $node = $this->panelStack->getNode($item->getPoint()->addPoint(new Point(0,-1)));

            if (!$node){
                return false;
            }

            if ($node instanceof NodeStack){
                return false;
            }
        }

        $this->nowBock->clearBock();
        $point->setCol($point->getCol() - 1);
        $this->nowBock->renderBock();
    }

    public function moveDown()
    {
        $this->nowBock->down($this->panelStack);
    }

    public function moveRight()
    {
        $point = $this->nowBock->getBock()->getStartPoint();

        $nodes = $this->nowBock->getBock()->getAllEffNode();

        foreach ($nodes as $item)
        {
            /**
             * @var INode $item
             */
            // 右边是null 或者 stack 则不能移动
            $node = $this->panelStack->getNode($item->getPoint()->addPoint(new Point(0,1)));

            if (!$node){
                return false;
            }

            if ($node instanceof NodeStack){
                return false;
            }
        }

        $this->nowBock->clearBock();
        $point->setCol($point->getCol() + 1);
        $this->nowBock->renderBock();

    }

    public function up()
    {
        // 获取旋转后的图形
        $bock = $this->nowBock->getBock()->rotate();

        // 检测是否可以旋转
        $nodes = $bock->getAllEffNode();

        foreach ($nodes as $node)
        {
            /**
             * @var INode $node
             */
            if ($this->panelStack->isStack($node->getPoint())) {
                return false;
            }
        }
        $this->nowBock->clearBock();
        // 可以旋转
        $this->nowBock->getBock()->setArea($bock->getArea());
        $this->nowBock->renderBock();
    }

    public function input()
    {
        $key = 0 + file_get_contents(ROOT.'bock.txt');

        switch ($key){
            case 4:
                $this->moveLeft();
                break;
            case 6:
                $this->moveRight();
                break;
            case 5:
                $this->moveDown();
                break;
            case 8:
                $this->up();
                break;
            default:
                break;
        }
    }

}